import Config from '@/config'
import request from '@/libs/request';

export default {
	uploadFile: filePath => { //文件上传
		const baseURL = process.env.NODE_ENV === 'development' ? Config.baseUrl.dev : Config.baseUrl.prod
		console.log(baseURL + '/upload')

		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: baseURL + '/upload',
				header: {
					'content-type': 'multipart/form-data', //multipart/form-data
				},
				// #ifdef MP-ALIPAY || MP-DINGTALK
				fileType: 'image',
				// #endif
				filePath: filePath,
				name: 'fileUpload',
				success: res => {
					console.log(res)

					if (res.statusCode === 200) {
						console.log(typeof res.data);
						const uploadRes = typeof res.data === 'object' ? res.data : JSON.parse(res.data)
						console.log('promiseUploadRes:', uploadRes);
						resolve(uploadRes)
					}
				},
				fail: err => {
					console.log(err)
					reject(err)
				}
			})

		})
	},
	downloadFile: url => {
		const baseURL = process.env.NODE_ENV === 'development' ? Config.baseUrl.dev : Config.baseUrl.prod
		console.log(baseURL + url)
		return new Promise((resolve, reject) => {
			uni.downloadFile({
				url: baseURL + url,
				header: {
					'authorization': uni.getStorageSync('loginRes').token || ''
				},
				success: res => {
					console.log(res)
					if (res.statusCode === 200) {
						resolve(res.tempFilePath);
					}
				},
				fail: err => {
					reject(err)
				}
			})
		})
	},
	requestAsync: (method = 'GET') => (url, opts = {}) => {
		let params = Object.assign({}, {
			data: {}
		}, opts);

		const contentType =
			method === 'POST' ?
			'application/x-www-form-urlencoded' :
			'application/json'

		const header = {
			'content-type': contentType,
			'authorization': uni.getStorageSync('loginRes').token || ''
		}

		const data = params.data;

		const responseType = params.responseType ? 'arrayBuffer' : 'text';

		let baseURL = '';

		// #ifndef MP-BAIDU
		baseURL = flyio.config.baseURL;
		// #endif

		// #ifdef MP-BAIDU
		baseURL = http.config.baseUrl;
		// #endif

		if (params.message) {
			uni.showLoading({
				title: params.message
			})
		}

		return new Promise((resolve, reject) => {
			uni.request({
				url: baseURL + url,
				method,
				data,
				header,
				responseType,
				success: res => {
					console.log('requestAsync success: ', res);

					if (res && res.statusCode === 200) {
						if (res.data.code && res.data.code !== 100) {
							uni.showToast({
								title: res.data.message,
								duration: 2000,
								icon: 'none',
							});
						}

						resolve(res.data)
					} else {
						if (res.data.code && res.data.code === 401) {
							uni.showToast({
								title: res.data.message,
								duration: 2000,
								icon: 'none',
							});

							uni.removeStorageSync('loginRes');

							setTimeout(() => {
								let url = '/pages/login/login';

								uni.reLaunch({
									url
								})
							}, 2000)
						}

						reject(res.data)
					}
				},
				fail: err => {
					console.log('requestAsync fail: ', res);

					reject(err);
				},
				complete: () => {
					if (params.message) {
						uni.hideLoading()
					}
				}
			})
		})
	}
}
